a = int(input())
fi = a // 1000
s = a // 100 % 10
t = a // 10 % 10
fo = a % 10
ma = 0
count=0
if len(str(a)) != 4:
    print("0")
else:
    if fi * 1000 + s * 100 + t * 10 + fo > ma:
        for i in range(1,(fi * 1000 + s * 100 + t * 10 + fo)+1):
            if (fi * 1000 + s * 100 + t * 10 + fo) % i == 0:
                count +=1
        if count == 2:
            ma = fi * 1000 + s * 100 + t * 10 + fo
    count = 0
    if fi * 100 + s * 10 + t > ma:
        for i in range(1,(fi * 100 + s * 10 + t)+1):
            if (fi * 100 + s * 10 + t) % i == 0:
                count +=1
        if count == 2:
            ma = fi * 100 + s * 10 + t
    count = 0
    if s * 100 + t * 10 + fo > ma:
        for i in range(1,(s * 100 + t * 10 + fo)+1):
            if (s * 100 + t * 10 + fo) % i == 0:
                count +=1
        if count == 2:
            ma = s * 100 + t * 10 + fo
    count = 0
    if fi * 10 + s > ma:
        for i in range(1,(fi * 10 + s)+1):
            if (fi * 10 + s) % i == 0:
                count +=1
        if count == 2:
            ma = fi * 10 + s
    count = 0
    if s * 10 + t > ma:
        for i in range(1,(s * 10 + t)+1):
            if (s * 10 + t) % i == 0:
                count +=1
        if count == 2:
            ma = s * 10 + t
    count = 0
    if t * 10 + fo > ma:
        for i in range(1,(t * 10 + fo)+1):
            if (t * 10 + fo) % i == 0:
                count +=1
        if count == 2:
            ma = t * 10 + fo
    count = 0
    if t > ma:
        for i in range(1,t+1):
            if t % i == 0:
                count +=1
        if count == 2:
            ma = t
    count = 0
    if fo > ma:
        for i in range(1,fo+1):
            if fo % i == 0:
                count +=1
        if count == 2:
            ma = t
    count = 0
    if s > ma:
        for i in range(1,s+1):
            if s % i== 0:
                count +=1
        if count == 2:
            ma = t
    count = 0
    if fi > ma:
        for i in range(1,fi+1):
            if fi % i == 0:
                count +=1
        if count == 2:
            ma = t
    count = 0
    print(ma)